﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Darkmoon.DX11Imported;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //LP_DXGI_SWAP_CHAIN_DESC pSCDesc = new LP_DXGI_SWAP_CHAIN_DESC();
            //pSCDesc.Value.BufferCount = 2;
            //pSCDesc.Value.BufferDesc.Width = (uint)ClientSize.Width;
            //pSCDesc.Value.BufferDesc.Height = (uint)ClientSize.Height;
            //pSCDesc.Value.BufferDesc.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
            //pSCDesc.Value.BufferDesc.RefreshRate.Numerator = 60;
            //pSCDesc.Value.BufferDesc.RefreshRate.Denominator = 1;
            //pSCDesc.Value.BufferUsage = DXGI_USAGE.DXGI_USAGE_RENDER_TARGET_OUTPUT;
            //pSCDesc.Value.OutputWindow = Handle;
            //pSCDesc.Value.SampleDesc.Count = 1;
            //pSCDesc.Value.SampleDesc.Quality = 0;
            //pSCDesc.Value.Windowed = 1;

            //LP_IDXGISwapChain ppSwapChain = new LP_IDXGISwapChain();
            //LP_ID3D11Device ppDevice = new LP_ID3D11Device();
            //LP_ID3D11DeviceContext ppDC = new LP_ID3D11DeviceContext();
            //DX11ImportLib_GlobalMethods.D3D11CreateDeviceAndSwapChain(
            //    null, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, IntPtr.Zero,
            //    new D3D11_CREATE_DEVICE_FLAG(), null, 0, DX_SDK_VERSIONS.D3D11_SDK_VERSION,
            //    pSCDesc, ppSwapChain: ppSwapChain, ppImmediateContext: ppDC, ppDevice: ppDevice);

            //pSwapChain = ppSwapChain.Value;
            //pDC = ppDC.Value;
            //pDevice = ppDevice.Value;
            //object pBackBufferRaw = null;
            //pSwapChain.GetBuffer(0, DX11_Guids_And_Consts.IID_ID3D11Texture2D, out pBackBufferRaw);
            //pBackBuffer = (ID3D11Texture2D)pBackBufferRaw;
            //LP_ID3D11RenderTargetView ppRTV = new LP_ID3D11RenderTargetView();
            //pDevice.CreateRenderTargetView(pBackBuffer, null, ppRTV);
            //pRTV = ppRTV.Value;
            //pDC.OMSetRenderTargets(1, new ID3D11RenderTargetView[] { pRTV });

            //D3D11_VIEWPORT vp;
            //vp.Width = ClientSize.Width;
            //vp.Height = ClientSize.Height;
            //vp.MinDepth = 0.0f;
            //vp.MaxDepth = 1.0f;
            //vp.TopLeftX = 0;
            //vp.TopLeftY = 0;
            //pDC.RSSetViewports(1, new D3D11_VIEWPORT[] { vp });

            //float[][] colors = new float[][]
            //{
            //    //rgba
            //    new float[]{ 1.0f, 0.0f, 0.0f, 1.0f },
            //    new float[]{ 0.0f, 1.0f, 0.0f, 1.0f },
            //    new float[]{ 0.0f, 0.0f, 1.0f, 1.0f },
            //};
            //int index = 0;
            //Random ran = new Random();
            //Timer timer = new Timer();
            //timer.Interval = 1000;
            //timer.Tick += (sender2, e2) =>
            //{
            //    float[] color = new float[] { (float)ran.NextDouble(), (float)ran.NextDouble(), (float)ran.NextDouble(), 1.0f };
            //    index = (index + 1) % colors.Length;
            //    pDC.ClearRenderTargetView(pRTV, color);
            //    pSwapChain.Present(0, new DXGI_PRESENT());
            //};
            //timer.Start();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //pSwapChain.Present(0, DXGI_PRESENT.DXGI_PRESENT_DO_NOT_SEQUENCE);
        }

        protected override void OnClosed(EventArgs e)
        {
            //if (pDC != null) pDC.ClearState();
            //base.OnClosed(e);
        }

        ID3D11Texture2D pBackBuffer = null;
        ID3D11Device pDevice = null;
        ID3D11DeviceContext pDC = null;
        IDXGISwapChain pSwapChain = null;
        ID3D11RenderTargetView pRTV = null;
        //LP_D3D_FEATURE_LEVEL pFL = new LP_D3D_FEATURE_LEVEL();
    }
}
